package com.boot.dog.rest.core;

import javax.validation.Valid;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.boot.dog.common.annotation.SysLog;
import com.boot.dog.common.util.ApiResult;
import com.boot.dog.entities.ao.RoleAo;
import com.boot.dog.entities.dto.RoleDto;
import com.boot.dog.entities.dto.RolePermissionDto;
import com.boot.dog.entities.pojo.Role;
import com.boot.dog.entities.vo.RoleVo;
import com.boot.dog.service.RoleService;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.annotations.ApiOperation;

import java.util.List;

/**
 * @Author: MaxLv
 * @Date: 2020/1/13
 */
@RestController
@RequestMapping("/core/role")
@AllArgsConstructor
public class RoleRest {

    private final RoleService roleService;

    @GetMapping("/getRoles")
    @ApiOperation(value = "角色分页")
    public ApiResult<IPage<RoleVo>> getRoles(Page page, RoleAo ao) {
        return roleService.getRoles(page, ao);
    }

    @PostMapping("/saveRole")
    @SysLog(desc = "添加角色")
    @ApiOperation(value = "添加角色")
    public ApiResult<Void> saveRole(@Valid @RequestBody RoleDto dto) {
        Role role = new Role();
        BeanUtil.copyProperties(dto, role);
        return roleService.saveRole(role);
    }

    @PutMapping("/editRole")
    @SysLog(desc = "编辑角色")
    @ApiOperation(value = "编辑角色")
    public ApiResult<Void> editRole(@Valid @RequestBody RoleDto dto) {
        Role role = new Role();
        BeanUtil.copyProperties(dto, role);
        return roleService.editRole(role);
    }

    @DeleteMapping("/delById/{id}")
    @SysLog(desc = "删除角色")
    @ApiOperation(value = "删除角色")
    public ApiResult<Void> delById(@PathVariable Integer id) {
        return roleService.delById(id);
    }

    @PutMapping("/editRolePerm")
    @SysLog(desc = "分配菜单权限")
    @ApiOperation(value = "分配菜单权限")
    public ApiResult<Void> editRolePerm(@RequestBody RolePermissionDto dto) {
        return roleService.editRolePerm(dto);
    }

    @GetMapping("/getAllRole")
    @ApiOperation(value = "获取当前用户")
    public ApiResult<List<Role>> getAllRole() {
        return roleService.getAllRole();
    }
}
